package com.yb.web.controller.system;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yb.framework.core.domain.AjaxObject;
import com.yb.framework.core.domain.PageResult;
import com.yb.framework.core.utils.StringUtil;
import com.yb.framework.log.annotation.Log;
import com.yb.framework.log.enums.OperateTypeEnum;
import com.yb.module.system.domain.SysContent;
import com.yb.web.controller.base.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping("/system/content")
@Api(tags = "内容管理")
public class SysContentController extends BaseController {
	
	Logger logger = LoggerFactory.getLogger(SysContentController.class.getName());
	
	private static String SYS_CONTENT = "system/content/content";
	private static String SYS_CONTENT_ADD = "system/content/content_add";
	private static String SYS_CONTENT_EDIT = "system/content/content_edit";
	private static String SYS_CONTENT_INFO = "system/content/content_info";
	
	@GetMapping()
	@PreAuthorize("hasAuthority('system:content')")
	public String main() {
		return SYS_CONTENT;
	}
	
	@GetMapping("/add_view")
	@PreAuthorize("hasAuthority('system:content:add')")
	public String addView() {
		return SYS_CONTENT_ADD;
	}
	
	@GetMapping("/edit_view/{id}")
	@PreAuthorize("hasAuthority('system:content:edit')")
	public String editView(@PathVariable("id") Long id, ModelMap model) {
		model.put("sysContent", sysContentService.getById(id));
		return SYS_CONTENT_EDIT;
	}
	
	@GetMapping("/info/{id}")
	@PreAuthorize("hasAuthority('system:content:info')")
	public String infoView(@PathVariable("id") Long id, ModelMap model) {
		model.put("sysContent", sysContentService.getById(id));
		return SYS_CONTENT_INFO;
	}
	
	/**
	 * 内容列表
	 */
	@PostMapping("/list")
	@PreAuthorize("hasAuthority('system:content:list')")
	@ApiOperation(value="分页查询", notes="分页查询内容信息")
	public @ResponseBody PageResult list(SysContent sysContent) {
		startPage();
		return getPageInfo(sysContentService.getPageByEntity(sysContent));
	}
	
	/**
	 * 新增内容
	 */
	@PostMapping("/add")
	@PreAuthorize("hasAuthority('system:content:add')")
	@ApiOperation(value="新增内容", notes="新增内容信息")
	@Log(label = "新增内容", operateType = OperateTypeEnum.ADD)
	public @ResponseBody AjaxObject add(SysContent sysContent) {
		return toAjax(sysContentService.insert(sysContent), "新增内容" + "[" + sysContent.getTitle() + "]");
	}
	
	/**
	 * 编辑内容
	 */
	@PostMapping("/edit")
	@PreAuthorize("hasAuthority('system:content:edit')")
	@ApiOperation(value="编辑内容", notes="编辑内容信息")
	@Log(label = "编辑内容", operateType = OperateTypeEnum.EDIT)
	public @ResponseBody AjaxObject edit(SysContent sysContent) {
		return toAjax(sysContentService.update(sysContent), "编辑内容" + "[" + sysContent.getTitle() + "]");
	}
	
	/**
	 * 删除内容
	 */
	@PostMapping("/delete")
	@PreAuthorize("hasAuthority('system:content:delete')")
	@ApiOperation(value="删除内容", notes="批量删除内容信息")
	@Log(label = "删除内容", operateType = OperateTypeEnum.DELETE)
	public @ResponseBody AjaxObject delete(@RequestParam("ids") String ids) {
		List<String> sysContentNames = sysContentService.getNamesByIds(StringUtil.str2LongArray(ids));
		return toAjax(sysContentService.deleteListByIds(StringUtil.str2LongArray(ids)), "批量删除内容" + sysContentNames);
	}
}